package com.cainaer.shop.controller;


import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.web.domain.AjaxResult;
import com.cainaer.shop.domain.CateType;
import com.cainaer.shop.service.ICateTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 门店类型 前端控制器
 * </p>
 *
 * @author Tina
 * @since 2020-09-03
 */
@RestController
@RequestMapping("/cateType/")
public class CateTypeController {

    private Logger logger = LoggerFactory.getLogger(CateTypeController.class);

    @Autowired
    private ICateTypeService cateTypeService;

    /**
     * 门店类型查询
     *
     * @param cateType
     * @return
     */
    @GetMapping("list")
    public AjaxResult queryCateType(CateType cateType) {
        logger.info("cateType", cateType);
        return AjaxResult.success(cateTypeService.queryCateType(cateType));
    }


    /**
     * 通过经营ID查询
     *
     * @param careId
     * @return
     */
    @GetMapping("select/{careId}")
    public AjaxResult selectCateTypeById(@PathVariable String careId) {
        if (StringUtils.isNotNull(careId)) {
            List<CateType> list = new ArrayList<>();
            String regEx = "[^0-9]+";
            Pattern pattern = Pattern.compile(regEx);
            //用定义好的正则表达式拆分字符串，把字符串中的数字留出来
            String[] cs = pattern.split(careId);
            for (String c : cs) {
                CateType cateType = cateTypeService.selectCateTypeById(Long.valueOf(c));
                if (StringUtils.isNotNull(cateType)) {
                    list.add(cateType);
                }
            }
            return AjaxResult.success(list);
        }
        return AjaxResult.error();
    }

}
